package com.jc.order.service.impl;

import com.jc.order.domain.SystemDictData;
import com.jc.order.dto.SystemDictDataDTO;
import com.jc.order.mapper.SystemDictDataMapper;
import com.jc.order.service.SystemDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class SystemDictDataServiceImpl implements SystemDictDataService {


    @Resource
    private SystemDictDataMapper systemDictDataMapper;


    @Override
//    @Cacheable(value = "dictCache", key = "#dictType")
    public List<SystemDictDataDTO> selectDictDataByDictType(String dictType) {
        // 构建查询条件：dictType匹配且状态为启用(0)
        Example example = new Example(SystemDictData.class, true, true);
        example.createCriteria()
                .andEqualTo("dictType", dictType)
                .andEqualTo("status", "0");

        // 查询并转换为DTO（空集合保护）
        return systemDictDataMapper.selectByExample(example).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }

    @Override
    public Map<String, List<SystemDictDataDTO>> selectDictDataByDictType(List<String> dictTypeList) {
        if (CollectionUtils.isEmpty(dictTypeList)) {
            return Collections.emptyMap();
        }

        // 构建批量查询条件
        Example example = new Example(SystemDictData.class, true, true);
        example.createCriteria()
                .andIn("dictType", dictTypeList)
                .andEqualTo("status", "0");

        // 分组转换（使用Stream分组收集器）
        return systemDictDataMapper.selectByExample(example).stream()
                .collect(Collectors.groupingBy(
                        SystemDictData::getDictType,
                        Collectors.mapping(this::convertToDto, Collectors.toList())
                ));
    }

    @Override
    public SystemDictDataDTO selectDictDataByDictTypeAndValue(String dictType, String value) {

        Example example = new Example(SystemDictData.class, true, true);
        example.createCriteria()
                .andEqualTo("dictType", dictType)
                .andEqualTo("value", value)
                .andEqualTo("status", "0");
        SystemDictData dictData=  systemDictDataMapper.selectOneByExample(example);

        return this.convertToDto(dictData);
    }

    @Override
    public SystemDictDataDTO selectDictDataByDictTypeAndLabel(String dictType, String label) {
        Example example = new Example(SystemDictData.class, true, true);
        example.createCriteria()
                .andEqualTo("dictType", dictType)
                .andEqualTo("label", label)
                .andEqualTo("status", "0");
        SystemDictData dictData=  systemDictDataMapper.selectOneByExample(example);

        return this.convertToDto(dictData);
    }

    /**
     * 实体转DTO（复用转换逻辑）
     */
    private SystemDictDataDTO convertToDto(SystemDictData entity) {
        if (entity!=null) {
            SystemDictDataDTO dto = new SystemDictDataDTO();
            BeanUtils.copyProperties(entity, dto);
            return dto;
        }
        return null;
    }
}
